package models

import (
	"taobao-merchandise/merchandiserpc/merchandiserpc"

	"gorm.io/gorm"
)

type User struct {
	gorm.Model
	Mobile    string `gorm:"type:char(11);not null;"`
	Password  string `gorm:"type:char(32);not null;"`
	HeadImage string `gorm:"type:varchar(500);not null;"`
	NickName  string `gorm:"type:varchar(30);not null;"`
}

type Shop struct {
	gorm.Model
	Title  string  `gorm:"type:varchar(30);not null;"`
	Image  string  `gorm:"type:varchar(500);not null;"`
	Price  float64 `gorm:"type:decimal(10,2);not null;"`
	UserId int64   `gorm:"type:int(11);not null;"`
}

func (s *Shop) ShopList(db *gorm.DB, shops []*merchandiserpc.ShopList, in *merchandiserpc.ShopListRequest) ([]*merchandiserpc.ShopList, error) {
	err2 := db.Debug().Model(&Shop{}).
		Select("shops .* , shops.id as shop_id , users.head_image , users.nick_name").
		Joins("left join users on users.id = shops.user_id").
		Scopes(Paginate(int(in.Page), int(in.Size))).
		Find(&shops).Error

	return shops, err2
}
